import {BaseData, MainData, PK, pkType, StatusData} from "../../coreModule/data/BaseData";
import {DataOccasion, field, occasion} from "../../coreModule/data/DataLoader";
import {DateUtils} from "../../../utils/DateUtils";
import {iLevelRepo, roleRepo, RoleType} from "./Role";

export enum AuthStatus {
	Pending = -1, Passed, Failed, Cancelled
}

export enum UserStatus {
	Normal, Banned
}

export enum GenderType {
	Male, Female
}
export enum EducationType {
	PrimarySchool,
	JuniorHighSchool,
	HighSchool,
	Undergraduate,
}

export const LongTimeTime = new Date(9999, 11, 31).getTime();
export const EducationNames = ["小学及以下", "初中", "高中", "本科及以上"];

export class Identification extends StatusData<AuthStatus> {

	@field(pkType())
	public userId: PK;

	@field(String)
	public name: string;
	@field
	public gender: GenderType = GenderType.Male;
	@field(Number)
	public birth: number;
	@field(String)
	public location: string; // 籍贯
	@field(String)
	public address: string; // 地址
	@field(String)
	public job: string; // 职业
	@field
	public education: EducationType = EducationType.PrimarySchool; // 学历
	@field(String)
	public specialty: string;
	@field(String)
	public cardNo: string;
	@field(Number)
	public startTime: number;
	@field(Number)
	public endTime: number;
	@field([String])
	public images: string[] = [];

	@field(Boolean)
	public isVeteran: boolean;
	@field(String)
	public cardAvatar: string; // 手持退役证照片
	// @field([String])
	// public veteranImages: string[] = [];

	@field(String)
	public inviteCode: string;
	@field(String)
	public phone: string;
	@field(String)
	public bankCard: string;
	@field
	public verifyCode: string = "";
	@field
	public showVerify: boolean=true;
	@field(String)
	public alipay: string;

	@field(Number)
	public createdAt: number;

	@field(Number)
	public status: AuthStatus;

	public user: User;

	constructor(index?: number, parent?: User) {
		super(index);
		this.user = parent;
	}

	// region 额外信息

	@field(String)
	@occasion(DataOccasion.Extra)
	public phoneStr: string

	@field(String)
	@occasion(DataOccasion.Extra)
	public educationStr: string;

	@field(String)
	@occasion(DataOccasion.Extra)
	public birthStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	public startTimeStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	public endTimeStr: string;

	public refresh() {
		if (this.phone?.length == 11) {
			const pre = this.phone.substr(0, 3);
			const suf = this.phone.substr(7, 4);
			this.phoneStr = `${pre}****${suf}`;
		} else this.phoneStr = "";

		this.educationStr = EducationNames[this.education];
		this.birthStr = DateUtils.time2DateStr(this.birth);
		this.startTimeStr = DateUtils.time2DateStr(this.startTime);
		this.endTimeStr = DateUtils.time2DateStr(this.endTime);
	}

	// endregion

}

const CertPictures = [undefined, "cert1.png", "cert2.png", "cert3.png"]

export class Certification extends StatusData<AuthStatus> {

	@field(pkType())
	public userId: PK;

	@field
	public name: string = "";
	@field(pkType())
	public roleId: PK;
	@field(String)
	public note: string;
	@field([String])
	public images: string[] = [];

	@field(pkType())
	public levelId: PK;

	@field(Number)
	public createdAt: number;

	@field(Number)
	public status: AuthStatus;
	@field(Number)
	public statusTime: number;
	@field(String)
	public statusDesc: string;

	public user: User;

	constructor(index?: number, parent?: User) {
		super(index);
		this.user = parent;
	}

	public get role() { return roleRepo().doc(this.roleId); }
	public get level() {
		return this.levelId ?
			iLevelRepo().doc(this.levelId) :
			iLevelRepo().getByLevel(0);
	}

	// region 额外信息

	@field(String)
	@occasion(DataOccasion.Extra)
	rolePicture: string
	@field(String)
	@occasion(DataOccasion.Extra)
	roleName: string;
	@field(RoleType)
	@occasion(DataOccasion.Extra)
	roleType: RoleType;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isInstructor: boolean = false;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isSchool: boolean = false;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isCompany: boolean = false;

	public refresh() {
		this.roleType = this.role?.type;
		this.rolePicture = CertPictures[this.roleType];
		if (this.roleType == RoleType.Instructor)
			this.roleName = this.levelId ?
				this.role.name + this.level.name : "未考核教官";
		else
			this.roleName = "";

		this.isInstructor = this.roleType == RoleType.Instructor;
		this.isSchool = this.roleType == RoleType.School;
		this.isCompany = this.roleType == RoleType.Company;
	}

	// endregion

}

export class User extends MainData {

	@field
	public nickName: string = "";
	@field
	public avatarUrl: string = "";
	@field
	public phone: string = "";
	@field
	public address: string = "";

	@field(String)
	public displayName: string

	@field(Identification)
	public identification: Identification;
	@field(Certification)
	public certification: Certification;

	@field
	public inviteCode: string = "";

	@field(Number)
	public createdAt: number;

	@field(Number)
	public status: UserStatus;
	@field(Number)
	public statusTime: number;
	@field(String)
	public statusDesc: string;

	// region 额外信息

	@field(String)
	@occasion(DataOccasion.Extra)
	phoneStr: string

	public refresh() {
		if (this.phone?.length == 11) {
			const pre = this.phone.substr(0, 3);
			const suf = this.phone.substr(7, 4);
			this.phoneStr = `${pre}****${suf}`;
		} else this.phoneStr = "";

		// this.certification?.refresh();
		// this.identification?.refresh();
	}

	// endregion

	// region 认证控制

	/**
	 * 是否身份认证通过
	 */
	public isIdentAuthed(judgePass: boolean = true) {
		return this.identification && (!judgePass ||
			this.identification.status == AuthStatus.Passed);
	}

	/**
	 * 是否资质认证通过
	 */
	public isCertAuthed(judgePass: boolean = true) {
		return this.certification && (!judgePass ||
			this.certification.status == AuthStatus.Passed);
	}

	/**
	 * 当前身份
	 */
	// @catchAsError(UserThrow.CertError, "请先进行资质认证!")
	public role() {
		return roleRepo().findOne({
			_id: this.certification?.roleId
		});
	}

	/**
	 * 设置个人信息
	 */
	public setupIdent(data: Identification) {
		data.user = this;
		this.identification = data;
	}

	/**
	 * 设置资质信息
	 */
	public setupCert(data: Certification) {
		data.user = this;
		this.certification = data;
	}

	// endregion

}

export class UserIndexData extends BaseData {
	// 关注的人
	@field(String)
	public openid: string;
	@field(String)
	public nickName: string;
	@field(String)
	public displayName: string;
	@field(String)
	public avatarUrl: string;
	@field(Number)
	public followCnt: number;
	@field(Number)
	public followerCnt: number;
	@field(Number)
	public publishCnt: number;
	@field(Certification)
	public certification: Certification;

	public static testData(){
		const res = new UserIndexData();
		res.nickName = "宫凤";
		res.displayName = "真实名称";
		res.avatarUrl = "https://img1.baidu.com/it/u=3008696966,846351368&fm=26&fmt=auto";
		res.followCnt = 34;
		res.followerCnt = 67;
		res.publishCnt = 3;

		res.certification = new Certification();
		res.certification.roleId = roleRepo().getByType(RoleType.Instructor)._id;
		res.certification.levelId = iLevelRepo().getByLevel(1)._id;

		res.certification.refresh();
		return [res];
	}
}
